#!/bin/bash

if [ $(id -u) -eq 0 ]; then
	
	if [ ! -d "/home/PDA_Users" ]; then
		mkdir /home/PDA_Users
	fi
	
	useradd -m -s /bin/bash -d "/home/PDA_Users/$1" $1
	result=$?
	
	if [ $result -eq 0 ]; then
		echo "Username $1 has been added to system!";
		mkdir /home/PDA_Users/$1/.vnc
		cp passwd /home/PDA_Users/$1/.vnc/
		cp xstartup /home/PDA_Users/$1/.vnc/
		chown -R $1.$1 /home/PDA_Users/$1/.vnc/
		touch /home/PDA_Users/$1/.Xauthority
		chown $1.$1 /home/PDA_Users/$1/.Xauthority
		
		echo "XAUTHORITY=/home/PDA_Users/$1/.Xauthority" >> /home/PDA_Users/$1/.profile
		echo 'vncserver -geometry 1600x900' >> /home/PDA_Users/$1/.profile
		echo 'Num=`ls .vnc/*.pid | sed s/.*:// | sed s/\.pid//`' >> /home/PDA_Users/$1/.profile
		echo 'cd /home/hong/workspace/vnc_javasrc/' >> /home/PDA_Users/$1/.profile
		echo 'java VncViewer PORT $((Num+5900))' >> /home/PDA_Users/$1/.profile
	elif [ $result -eq 9 ]; then
		echo "Username $1 already in use!"
	else
		echo "Failed to add user $1!"
	fi
	
else
	echo "Only root may add a user to the system"
	exit 2
fi
